#define _CRT_SECURE_NO_WARNINGS  1
#include<stdio.h>
#include<stdlib.h>
int count1, count2;
struct Node
{
	int data;
	struct Node* next;
};
typedef struct Node L;
L* Listcreate(L* head,int* count);
L* Listcreate_(L* head, int count);
L* sort(L* s, L* s1, L* s2);
int main()
{
	L* s1=NULL, * s2=NULL;
	L* p, * q, * r1 = s1, * r2 = s2;
	int count = 0;
	s1=Listcreate(s1,&count1);
	s2 = Listcreate(s2,&count2);
	L* s = NULL;
	s=Listcreate_(s, count1 + count2);
	s=sort(s, s1, s2);
	if (s != NULL)
	{
		L* m = s;
		for (m; m->next != NULL; m = m->next)
		{
			if (!count)
			{
				printf("%d", m->data);
				count++;
			}
			else
				printf(" %d", m->data);
		}
		printf(" %d", m->data);
	}
	else
	{
		printf("NULL");
	}
	return 0;
}
L* Listcreate(L* head,int* count)
{
	L* p, * q=head;
	int data;
	scanf("%d", &data);
	while (data != -1)
	{
		p = (L*)malloc(sizeof(L));
		p->data = data;
		if (head == NULL)
		{
			head = p;
			q = p;
		}
		else
		{
			q->next = p;
			q = q->next;
		}
		(*count)++;
		scanf("%d", &data);
	}
	p = (L*)malloc(sizeof(L));
	p->data = -1;
	p->next = NULL;
	if(q!=NULL)
		q->next = p;
	return head;
}
L* Listcreate_(L* head, int count)
{
	L* p = NULL;
	for (int i = 0; i < count; i++)
	{
		p = (L*)malloc(sizeof(L));
		p->next = head;
		head = p;
	}
	return head;
}
L* sort(L* s, L* s1, L* s2)
{
	L* a = s1, * b = s2,* c = s;
	if (a == NULL && b == NULL)
	{
		s = NULL;
	}
	else if(a!=NULL&&b!=NULL)
	{
		while (a->data != -1 && b->data != -1)
		{
			if (a->data > b->data)
			{
				c->data = b->data;
				c = c->next;
				b = b->next;
			}
			else if (a->data < b->data)
			{
				c->data = a->data;
				c = c->next;
				a = a->next;
			}
			else
			{
				c->data = a->data;
				c = c->next;
				a = a->next;
			}
		}
		while (a->data != -1)
		{
			c->data = a->data;
			a = a->next;
			c = c->next;
		}
		while (b->data != -1)
		{
			c->data = b->data;
			b = b->next;
			c = c->next;
		}
	}
	else
	{
		if (a == NULL)
		{
			while (b->data != -1)
			{
				c->data = b->data;
				b = b->next;
				c = c->next;
			}
		}
		else
		{
			while (a->data != -1)
			{
				c->data = a->data;
				a = a->next;
				c = c->next;
			}
		}
	}
	return s;
}


//#include<stdio.h>
//#include<stdlib.h>
//int count1, count2;
//struct Node
//{
//	int data;
//	struct Node* next;
//};
//typedef struct Node L;
//L* Listcreate(L* head, int* count);
//L* Add(L* s1, L* s2);
//L* sort(L* head);
//int main()
//{
//	L* s1 = NULL, * s2 = NULL;
//	L* p, * q, * r1 = s1, * r2 = s2;
//	int count = 0;
//	s1 = Listcreate(s1, &count1);
//	s2 = Listcreate(s2, &count2);
//	L* s = NULL;
//	s = Add(s1, s2);
//	if (s != NULL)
//	{
//		L* m = s;
//		for (m; m->data != -1; m = m->next)
//		{
//			if (!count)
//			{
//				printf("%d", m->data);
//				count++;
//			}
//			else
//				printf(" %d", m->data);
//		}
//	}
//	else
//	{
//		printf("NULL");
//	}
//	return 0;
//}
//L* Listcreate(L* head, int* count)
//{
//	L* p, * q = head;
//	int data;
//	scanf("%d", &data);
//	while (data != -1)
//	{
//		p = (L*)malloc(sizeof(L));
//		p->data = data;
//		if (head == NULL)
//		{
//			head = p;
//			q = p;
//		}
//		else
//		{
//			q->next = p;
//			q = q->next;
//		}
//		(*count)++;
//		scanf("%d", &data);
//	}
//	p = (L*)malloc(sizeof(L));
//	p->data = -1;
//	p->next = NULL;
//	if (q != NULL)
//		q->next = p;
//	return head;
//}
//L* Add(L* s1, L* s2)
//{
//	if (s1 == NULL && s2 == NULL)
//		return NULL;
//	L* s = s1, * p = s;
//	while (p->data != -1)
//	{
//		p = p->next;
//	}
//	p->data = s2->data;
//	p->next = s2->next;
//	sort(s);
//	return s;
//}
//L* sort(L* head)
//{
//	L* p, * q;
//	int tmp, i;
//	for (i = 0; i < count1 + count2; i++)
//	{
//		p = head;
//		q = head->next;
//		while (p->next->next != NULL)
//		{
//			if (p->data > q->data)
//			{
//				tmp = p->data;
//				p->data = q->data;
//				q->data = tmp;
//			}
//			p = p->next;
//			q = q->next;
//		}
//	}
//}